﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using com.ubosi.model.Entity;
using com.ubosi.spider.core.Net;
using com.ubosi.spider.config;
using System.Text.RegularExpressions;
using System.IO;

namespace com.ubosi.spider.hotel.HoteInfo
{
    public class SpiderElongHotelInfo: ISpiderHotelInfo
    {        
        private NetSpider _NetSpider = null;
        private SpiderHotelInfoConfig _Config = null;

        public SpiderElongHotelInfo(NetSpider netSpider,SpiderHotelInfoConfig config)
        {
            _NetSpider = netSpider;
            _Config = config;
        }

        private Hotel ParseHtml(string htmlData)
        {
            Hotel hotel = new Hotel();
            
            MatchCollection mc = Regex.Matches(htmlData, _Config.HotelPicRegx);
            if (mc.Count == 1)
            {
                hotel.HotelPic = mc[0].Result("$" + (mc[0].Groups.Count - 1).ToString());
            }

            mc = Regex.Matches(htmlData, _Config.HotelStarRegx);
            if (mc.Count == 1)
            {
                hotel.HotelStar = int.Parse(mc[0].Result("$"+(mc[0].Groups.Count-1).ToString()));
            }

            mc = Regex.Matches(htmlData, _Config.HotelAddressRegx);
            if (mc.Count == 1)
            {
                hotel.HotelAddress = mc[0].Result("$" + (mc[0].Groups.Count-1).ToString());
            }

            mc = Regex.Matches(htmlData, _Config.HotelLatRegx);
            if (mc.Count == 1)
            {
                hotel.Lat = float.Parse(mc[0].Result("$" + (mc[0].Groups.Count-1).ToString()));
            }

            mc = Regex.Matches(htmlData, _Config.HotelLngRegx);
            if (mc.Count == 1)
            {
                hotel.Lng = float.Parse(mc[0].Result("$" + (mc[0].Groups.Count-1).ToString()));
            }

            mc = Regex.Matches(htmlData, _Config.HotelIntroRegx);
            if (mc.Count == 1)
            {
                hotel.HotelIntro = mc[0].Result("$" + (mc[0].Groups.Count-1).ToString());
            }

            mc = Regex.Matches(htmlData, _Config.ServicesRegx);
            if (mc.Count == 1)
            {
                hotel.Services = mc[0].Result("$" + (mc[0].Groups.Count-1).ToString());
            }

            mc = Regex.Matches(htmlData, _Config.MeetingRegx);
            if (mc.Count == 1)
            {
                hotel.Meeting = mc[0].Result("$" + (mc[0].Groups.Count-1).ToString());
            }

            mc = Regex.Matches(htmlData, _Config.EntertainmentRegx);
            if (mc.Count == 1)
            {
                hotel.Entertainment = mc[0].Result("$" + (mc[0].Groups.Count-1).ToString());
            }

            mc = Regex.Matches(htmlData, _Config.FacilitiesRegx);
            if (mc.Count == 1)
            {
                hotel.Facilities = mc[0].Result("$" + (mc[0].Groups.Count-1).ToString());
            }

            mc = Regex.Matches(htmlData, _Config.FoodRegx);
            if (mc.Count == 1)
            {
                hotel.Food = mc[0].Result("$" + (mc[0].Groups.Count-1).ToString());
            }

            mc = Regex.Matches(htmlData, _Config.HotelNameRegx);
            if (mc.Count == 1)
            {
                hotel.HotelName = mc[0].Result("$" + (mc[0].Groups.Count-1).ToString());
            }           

            mc = Regex.Matches(htmlData, _Config.PromotionRegx);
            if (mc.Count == 1)
            {
                hotel.Promotion = mc[0].Result("$" + (mc[0].Groups.Count-1).ToString());
            }

            mc = Regex.Matches(htmlData, _Config.SnowntownNameRegx);
            if (mc.Count == 1)
            {
                hotel.SnowntownId = mc[0].Result("$" + (mc[0].Groups.Count - 1).ToString());
            }

            return hotel;
        }

        private bool IsValidSpider(string htmlData)
        {
            MatchCollection mc = Regex.Matches(htmlData, _Config.IsValidRegx);
            string hotelid = string.Empty;
            if (mc.Count == 1)
            {
                hotelid = mc[0].Result("$1").Trim();
            }
            return !string.IsNullOrEmpty(hotelid);
        }

        #region ISpiderHotelInfo 成员

        public Hotel GetHotelInfo(string hotelId)
        {
            try
            {
                string htmlData = _NetSpider.GetHtmlDocument(string.Format(_Config.SpiderUrl, hotelId), Encoding.Default);


                if (!IsValidSpider(htmlData))
                {
                    throw new Exception("抓取内容不合法");
                }

                return ParseHtml(htmlData);
            }
            catch (Exception ex)
            {
                throw ex;
            }            
        }

        public IList<string> GetHotelImage(string hotelId)
        {

            string imageXmlFileUrl = string.Format("http://data.elong.com/hotels/VirtualTour/{0}_cn.xml", hotelId);
            string LocalXmlFilePath = @"C:\工作目录\游博网\SourceCode\UBOSI\Web\images\HotelXml\";

            if (!File.Exists(LocalXmlFilePath + hotelId + ".xml"))
            {
                string fileName = ImageSpider.Download(hotelId + ".xml", LocalXmlFilePath, imageXmlFileUrl);
            }
            return null;
        }
        #endregion
    }
}
